package com.example.mylibrarybase.yumengmeng20.utils;

import android.content.Context;
import android.os.Handler;

import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.utils.UtilsToast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by 35876 于萌萌
 * 创建日期: 16:19 . 2016年11月01日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public   class mySocket{
    Context mContext;
    Handler handler;
    private ExecutorService sExecutorService;
    Socket socket;
    ServerSocket serverSocket ;

    public mySocket(Context context,Handler handler) {
        mContext = context;
        this.handler = handler;
    }

    public  mySocket connectServerWithTCPSocket(final String ip, final String port, final String manseeger) {
            if (null==sExecutorService){
                sExecutorService = Executors.newFixedThreadPool(5);
            }
           // SendSocket sendSocket = new SendSocket(manseeger);
            sExecutorService.execute(new SendSocket(ip,port, manseeger,handler));
        return this;
    }



    public   mySocket ServerReceviedByTcp(final String port) {
        // 声明一个ServerSocket对象
        try {
            // 创建一个ServerSocket对象，并让这个Socket在1989端口监听
            serverSocket = new ServerSocket(Integer.parseInt(port));
            if (null==sExecutorService){
                sExecutorService = Executors.newFixedThreadPool(5);
            }
         //   while (true){
                sExecutorService.execute(new getSocket(mContext));

           // }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    public class  SendSocket implements Runnable {
        String ip;
        String port;
        String manseeger;
        Handler handler;

        public SendSocket(String ip, String port, String manseeger, Handler handler) {
            this.ip=ip;
            this.port=port;
            this.manseeger = manseeger;
            this.handler =handler;
        }

        @Override
        public void run() {
            InputStream inputStream = Utilssocket.getStringStream(manseeger);
            // 获取Socket的OutputStream对象用于发送数据。
            OutputStream outputStream = null;
            try {
                socket = new Socket(ip, Integer.parseInt(port));
                outputStream = socket.getOutputStream();
                // 创建一个byte类型的buffer字节数组，用于存放读取的本地文件
                byte buffer[] = new byte[4 * 1024];
                int temp = 0;
                // 循环读取文件
                while ((temp = inputStream.read(buffer)) != -1) {
                    // 把数据写入到OuputStream对象中
                    outputStream.write(buffer, 0, temp);
                }
                // 发送读取的数据到服务端
                outputStream.flush();
                outputStream.close();
                socket.close();
               // socket.close();
                LogUtils.i("发送成功"+"内容是"+manseeger);
            } catch (IOException e1) {
                e1.printStackTrace();
            }


        }
    }

    public class getSocket implements Runnable {
       public  Context context;
        public getSocket(Context context){
            this.context = context;
        }
        @Override
        public void run() {
            Socket socket = null;
                // 调用ServerSocket的accept()方法，接受客户端所发送的请求，
                // 如果客户端没有发送数据，那么该线程就停滞不继续
            try {
                while (true){
                    socket = serverSocket.accept();
                    // 从Socket当中得到InputStream对象
                    LogUtils.i("来消息了");
                    InputStream inputStream = socket.getInputStream();


                    byte buffer[] = new byte[1024 * 4];
                    int temp = 0;
                    // 从InputStream当中读取客户端所发送的数据
                    final StringBuilder stringBuilder = new StringBuilder();
                    while ((temp = inputStream.read(buffer)) != -1) {
                        stringBuilder.append(new String(buffer, 0, temp));

                    }
                    LogUtils.i(stringBuilder + "");
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            UtilsToast.showToast(context, "收到内容是" + stringBuilder);
                        }
                    });
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
